<template>
  <VPage class="app-container">
    <VTable ref="tableRef" v-bind="tableOptins">
      <template #form="{ form }">
        <SelectWarehouse  class="selectClass" v-model="form.warehouse_id" placeholder="仓库" clearable :multiple="true"></SelectWarehouse>
        <VGroup>
          <el-select class="selectClass" style="max-width: 100px" v-model="form.personRange.type" placeholder="请选择">
            <el-option v-for="item in personOptions" :key="item.value" :label="item.label"
              :value="item.value"></el-option>
          </el-select>
          <select-personnel class="w-40" v-model="form.personRange.value"></select-personnel>
        </VGroup>
        <VGroup>
          <el-select class="selectClass" v-model="form.timeRange.type" style="max-width: 100px">
            <el-option v-for="item in timeOption" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
          <el-date-picker v-model="form.timeRange.value" type="daterange" range-separator="至" start-placeholder="开始时间"
            end-placeholder="结束时间" value-format="YYYY-MM-DD HH:mm:ss" :default-time="defaultTime()" style="max-width: 300px" />
        </VGroup>
        <el-select v-model="form.status" placeholder="是否启用">
          <el-option v-for="item in options['system.headGoodConfig']?.[0].headGoodConfigStatus" :key="item.value"
            :label="item.label" :value="item.value">
          </el-option>
        </el-select>
        <el-input v-model="form.name" placeholder="任务名称" class="w-40"></el-input>
      </template>
      <template #toolbar_btns>
        <el-button type="primary" @click="open('add')"><svg-icon class="text-white mr-1" icon-class="add"/>新增</el-button>
      </template>
    </VTable>
    <ComModal ref="ComModalRef" :options="options" :wareDataList="wareDataList" @query="query"></ComModal>
  </VPage>
</template>
<script setup name="SystemHead-good" lang="jsx">
import { ElMessage, ElMessageBox } from 'element-plus'
import { ref, reactive, toRefs } from 'vue'
import { getEnums } from '@/api/public'
import { queryHeadGoodConfigList } from './api'
import ComModal from './components/ComModal.vue'
import SelectWarehouse from '@/components/SelectWarehouse'

const timeOption = [
  { label: "创建时间", value: "create_time" },
  { label: "修改时间", value: "update_time" },
]

const personOptions = [
  { label: "创建人", value: "create_user_id" },
  { label: "修改人", value: "update_user_id" },
]

const tableRef = ref()
const tableOptins = reactive({
  rowConfig: { height: 60 },
  formConfig: {
    data: {
      personRange: { type: 'create_user_id', value: [], range: true },
      timeRange: { type: 'create_time', value: [], range: true },
    }
  },
  columns: [
    {
      type:"seq", title: '序号',width:60
    },
    {
      field: 'name', title: '任务名称'
    },
    {
      field: 'warehouse_name', title: '仓库'
    },
    {
      field: 'priority_level', title: '优先级',width:80
    },
    {
      field: 'status_cn', title: '是否启用',width:80
    },
    {
      field: 'create_user', title: '创建人'
    },
    {
      field: 'create_time', title: '创建日期'
    },
    {
      field: 'update_user', title: '最近修改人'
    },
    {
      field: 'update_time', title: '修改日期'
    },
    {
      title: '操作', width: "160", fixed: "right", slots: {
        default: ({ row }) => {
          return <div class="table-option">
            <div>
              <el-link type="primary" underline={false} class="mr-2" onClick={() => open('view', row.id)}>查看</el-link>
              <el-link type="primary" underline={false} class="mr-2" onClick={() => open('edit', row.id)}>修改</el-link>
            </div>
          </div >
        }
      }
    }
  ],
  proxyConfig: {
    seq: true,
    props: {
      result: 'data',
      total: 'total',
    },
    ajax: {
      query: async ({ page, form }) => {
        const res = await queryHeadGoodConfigList({ ...form, ...page })
        return new Promise(function (resolve) {
          resolve({
            data: res.data,
            total: res.total
          })
        })
      }
    }
  }
})
const query = () => {
  tableRef.value.query()
}

const data = reactive({
  options: []
})
const { options } = toRefs(data)

// 获取枚举类
const fetchOption = async () => {
  getEnums({
    version: "v1", "name": [
      "system.headGoodConfig"
    ]
  }).then(res => {
    options.value = res.data
  })
}
fetchOption()

const ComModalRef = ref()
const open = (type, id) => {
  ComModalRef.value.open(type, id)
}

</script>